package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.domain.User;
import com.itheima.myexception.BusinessException;
import com.itheima.service.UserService;
import com.itheima.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("/login/{username},{password}")
    public ModelAndView login(@PathVariable String username, @PathVariable String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(user);
        User login = userService.login(user);
        System.out.println(login);
        ModelAndView modelAndView = new ModelAndView();
        if (login != null) {
            modelAndView.addObject("msg", "登陆成功");
            modelAndView.setViewName("/index.jsp");
            modelAndView.addObject("user", login);
        } else {
            modelAndView.addObject("msg", "登录失败");
            modelAndView.setViewName("/index.jsp");
        }
        return modelAndView;
    }

    @PostMapping("/addStudent/{name},{age}")
    public String addStudent(@PathVariable String name, @PathVariable Integer age) {
        System.out.println("post");
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        userService.addStudent(student);
        return "successful";
    }

    @DeleteMapping("/deleteStudent/{id}")
    public String deleteStudent(@PathVariable String id) {
        userService.deleteStudent(id);
        return "successful";
    }

    @PutMapping("/updateStudent/{id},{name},{age}")
    public String updateStudent(@PathVariable String id, @PathVariable String name, @PathVariable Integer age) {
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        userService.updateStudent(student);
        return "successful";
    }

    @RequestMapping("exception")
    public void updateStudent() {
        EmailUtil.sendMail("wuzhuangzhuang199@163.com","有异常，回来加班","加班信息");
        throw new BusinessException("没啥事，就是想抛个异常");
    }


}



   
   
   
                                  
   
   